package com.software.shipper.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.software.common.CommonUtils;
import com.software.form.billform.dao.BillForm;
import com.software.form.billform.dao.BillFormTransaction;
import com.software.form.cargobooking.dao.CargoBookingTransaction;

public class QueryBillServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String user = (String) request.getSession().getAttribute("user");
		BillFormTransaction bft = (BillFormTransaction)CommonUtils.getBean("billFormTransaction");
		List<BillForm> list = bft.getBills(user);
		JSONObject json = new JSONObject();
		if(list.size()>0){
			try {
				for(int i=0; i < list.size();i++){
					JSONArray array = new JSONArray();
					array.put(list.get(i).getNo());
					array.put(list.get(i).getCompany());
					array.put(list.get(i).getConsigneeName());
					array.put(list.get(i).getConsigneeAddress());
					array.put(list.get(i).getSagentEmail());
					json.put("bills", array);
				}
				CommonUtils.outputJsonData(json.toString(), response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		BillFormTransaction bft = (BillFormTransaction)CommonUtils.getBean("billFormTransaction");
		JSONObject o = CommonUtils.readJSONStringFromRequestBody(request, response);
		try {
			String no = o.getString("no");
			BillForm bf = bft.getBillByNo(no);
			JSONObject json = new JSONObject();
			json.put("company", bf.getCompany());
			json.put("conName", bf.getConsigneeName());
			json.put("conAddr", bf.getConsigneeAddress());
			json.put("bank", bf.getDepositBank());
			json.put("account", bf.getAccount());
			json.put("vess", bf.getVesselName());
			json.put("voy", bf.getVoyage());
			json.put("pol", bf.getLoadingPort());
			json.put("dest", bf.getDestinationPort());
			json.put("no", bf.getNo());
			json.put("dp", bf.getDeliverPrivision());
			json.put("ocean", bf.getOceanFrieight());
			json.put("pou", bf.getUnloadingPlace());
			json.put("ad", bf.getArrivalDate());
			json.put("iyd", bf.getIntoYardDate());
			json.put("mark", bf.getMarkAndNo());
			json.put("cargo", bf.getCargoName());
			json.put("quantity", bf.getQuantity());
			json.put("weight", bf.getQuantity());
			json.put("volume", bf.getVolume());
			json.put("sagentEmail", bf.getSagentEmail());
			json.put("sEmail", bf.getShipperEmail());
			CommonUtils.outputJsonData(json.toString(), response);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
